package br.com.lazyprogrammer.social.infestit.network.task;

import android.content.Context;
import android.os.AsyncTask;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;

import br.com.lazyprogrammer.social.infestit.network.JSONConstants;
import br.com.lazyprogrammer.social.infestit.network.Request;
import br.com.lazyprogrammer.social.infestit.network.URLParams;
import br.com.lazyprogrammer.social.infestit.network.builder.URLBuilder;
import br.com.lazyprogrammer.social.infestit.network.listener.LoginListener;
import br.com.lazyprogrammer.social.infestit.system.message.Message;
import br.com.lazyprogrammer.social.infestit.util.Parser;

/**
 * Created by Andre on 10/09/2014.
 */
public class LoginTask extends AsyncTask<String, Void, Request.Response> {

    private Context context;
    private LoginListener listener;
    private Request.Callback callback;

    public LoginTask(Context context, Request.Callback callback, LoginListener listener) {
        this.context = context;
        this.callback = callback;
        this.listener = listener;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        callback.onStart(getClass());
    }

    @Override
    protected Request.Response doInBackground(String... params) {
        // TODO Auto-generated method stub
        String username	= params[0];
        String password	= params[1];
        Parser parser	= new Parser();
        Request.Response status;
        try {
            JSONObject parent	= new Request().post(new URLBuilder().buildUrlSystem(URLBuilder.Type.ME), new URLParams().buildLoginParams(username, password));
            status	= Request.Response.values()[parser.parseResponse(parent)];
            if (status.equals(Request.Response.Success))
                listener.onLoginSuccessful(parser.parseUser(parent.getJSONObject(JSONConstants.OBJECT).getJSONObject(JSONConstants.USER)), password);
        } catch (SocketTimeoutException e) {
            // TODO Auto-generated catch block
            status	= Request.Response.SocketTimeout;
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            status	= Request.Response.UnsupportedEncoding;
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            status	= Request.Response.ClientProtocol;
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            status	= Request.Response.IllegalState;
            e.printStackTrace();
        } catch (NullPointerException e) {
            // TODO Auto-generated catch block
            status	= Request.Response.NullPointer;
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            status	= Request.Response.IO;
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            status	= Request.Response.JSON;
            e.printStackTrace();
        }
        return status;
    }

    @Override
    protected void onPostExecute(Request.Response result) {
        if (isCancelled())
            callback.onCancel(getClass());
        else {
            if (result.equals(Request.Response.Success))
                callback.onSuccess(getClass(), result);
            else
                callback.onError(getClass(), result, Message.getException(context, result));
        }
    }

}
